//////////////////////////////////////////////////////////////////////////////////////
//
//  Copyright (c) 2014-present, Egret Technology.
//  All rights reserved.
//  Redistribution and use in source and binary forms, with or without
//  modification, are permitted provided that the following conditions are met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//     * Neither the name of the Egret nor the
//       names of its contributors may be used to endorse or promote products
//       derived from this software without specific prior written permission.
//
//  THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
//  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
//  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
//  IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
//  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
//  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
//  OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
//  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
//  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
//  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//////////////////////////////////////////////////////////////////////////////////////

class Main extends eui.UILayer {
    private theme: eui.Theme;


    protected createChildren(): void {
        super.createChildren();

        // egret.ImageLoader.crossOrigin = "anonymous";
        egret.lifecycle.addLifecycleListener((context) => {
            // custom lifecycle plugin
        })

        egret.lifecycle.onPause = () => {
            egret.ticker.pause();
        }

        egret.lifecycle.onResume = () => {
            egret.ticker.resume();
        }

        //inject the custom material parser
        //注入自定义的素材解析器
        let assetAdapter = new AssetAdapter();
        egret.registerImplementation("eui.IAssetAdapter", assetAdapter);
        egret.registerImplementation("eui.IThemeAdapter", new ThemeAdapter());

        //微信分享
        platform.showShareMenu()
        // initialize the Resource loading library
        //初始化Resource资源加载库



        RES.loadConfig("default.res.json", "resource/");
        //RES.loadConfig("default.res.json", "https://praying715.cn/download/resource/");
        //RES.loadConfig("default.res.json", "http://172.16.18.54:6600/download/resource/");



        //console.log("开始资源加载中");
        RES.addEventListener(RES.ResourceEvent.CONFIG_COMPLETE, this.onConfigComplete, this);

    }
    /**
       * 配置文件加载完成,开始预加载皮肤主题资源和preload资源组。
       * Loading of configuration file is complete, start to pre-load the theme configuration file and the preload resource group
       */
    private onConfigComplete(event: RES.ResourceEvent): void {
        //console.log("添加资源侦听");
        RES.removeEventListener(RES.ResourceEvent.CONFIG_COMPLETE, this.onConfigComplete, this);
        RES.addEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);
        RES.addEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, this.onResourceLoadError, this);
        RES.addEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onResourceProgress, this);
        RES.addEventListener(RES.ResourceEvent.ITEM_LOAD_ERROR, this.onItemLoadError, this);
        RES.loadGroup("preload");
    }

    private init(): void {
        GlobalData.GameStage = this.stage;
        GlobalData.GameStage_width = this.stage.stageWidth; //egret.MainContext.instance.stage.stageWidth;
        GlobalData.GameStage_height = this.stage.stageHeight; //egret.MainContext.instance.stage.stageHeight;
    }
    /* 主题文件加载完成,开始预加载
  * Loading of theme configuration file is complete, start to pre-load the 
  */
    private onThemeLoadComplete(): void {
        this.theme.removeEventListener(eui.UIEvent.COMPLETE, this.onThemeLoadComplete, this);
        RES.loadGroup("loading");
    }

    /**
       * preload资源组加载完成
       * preload resource group is loaded
       */
    private onResourceLoadComplete(event: RES.ResourceEvent): void {

        if (event.groupName == "preload") {
            // console.log("资源组" + event.groupName + "加载完成");
            //加载皮肤主题配置文件,可以手动修改这个文件。替换默认皮肤。
            this.theme = new eui.Theme("resource/default.thm.json", this.stage);
            this.theme.addEventListener(eui.UIEvent.COMPLETE, this.onThemeLoadComplete, this);
        }
        else if (event.groupName == "loading") {
            // console.log("资源组" + event.groupName + "加载完成");
            RES.loadGroup("game");
        }
        else if (event.groupName == "game") {
            // console.log("资源组" + event.groupName + "加载完成");
            this.init();
            //this.addChild(new LoginMain());
            //UIManager.getInstance().startGame();
            SceneManager.instance.setStage(this);
            SceneManager.instance.SitchScene(SceneName.登录场景);
            this.clear();
        }
    }
    private clear(): void {
        // console.log("清除资源侦听");
        RES.removeEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);
        RES.removeEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, this.onResourceLoadError, this);
        RES.removeEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onResourceProgress, this);
        RES.removeEventListener(RES.ResourceEvent.ITEM_LOAD_ERROR, this.onItemLoadError, this);
        this.theme = null;

    }
    /**
     * 资源组加载出错
     *  The resource group loading failed
     */
    private onItemLoadError(event: RES.ResourceEvent): void {
        console.warn("Url:" + event.resItem.url + " has failed to load");
    }
    /**
     * 资源组加载出错
     * Resource group loading failed
     */
    private onResourceLoadError(event: RES.ResourceEvent): void {
        //TODO
        console.warn("Group:" + event.groupName + " has failed to load");
        //忽略加载失败的项目
        //ignore loading failed projects
        this.onResourceLoadComplete(event);
    }
    /**
     * preload资源组加载进度
     * loading process of preload resource
     */
    private onResourceProgress(event: RES.ResourceEvent): void {

    }
}
